package logic

import (
	"context"
	"fmt"
	"strings"
	"teatch/model/mysql"

	"teatch/teacherrpc/internal/svc"
	"teatch/teacherrpc/teacherrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectEvaluateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectEvaluateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectEvaluateLogic {
	return &CollectEvaluateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 课程评价
func (l *CollectEvaluateLogic) CollectEvaluate(in *teacherrpc.CollectEvaluateRequest) (*teacherrpc.CollectEvaluateResponse, error) {
	// todo: add your logic here and delete this line

	var eva mysql.Evaluate
	mysql.DB.Where("collect_id=?", in.CollectId).Find(&eva)
	if in.CollectId == 0 {
		return nil, fmt.Errorf("该课程不存在")
	}

	if strings.Contains(in.Content, "虚假") || strings.Contains(in.Content, "垃圾") {
		return nil, fmt.Errorf("检测评论敏感词已被拦截 ")
	}

	data := mysql.Evaluate{
		UserId:    int(in.UserId),
		CollectId: int(in.CollectId),
		Content:   in.Content,
	}
	mysql.DB.Create(&data)

	return &teacherrpc.CollectEvaluateResponse{
		EvaluateId: int64(data.ID),
	}, nil
}
